package com.tcpudp.controller;

import com.tcpudp.model.Connection;
import com.tcpudp.model.ServerConfig;
import com.tcpudp.service.ConnectionService;
import com.tcpudp.service.ServerManagerService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/servers")
public class ServerController {
    
    private final ServerManagerService serverManagerService;
    private final ConnectionService connectionService;
    
    public ServerController(ServerManagerService serverManagerService, ConnectionService connectionService) {
        this.serverManagerService = serverManagerService;
        this.connectionService = connectionService;
    }
    
    @GetMapping
    public ResponseEntity<List<ServerConfig>> getAllServers() {
        return ResponseEntity.ok(serverManagerService.getAllServers());
    }
    
    @GetMapping("/{serverId}")
    public ResponseEntity<ServerConfig> getServer(@PathVariable String serverId) {
        ServerConfig config = serverManagerService.getServer(serverId);
        if (config == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(config);
    }
    
    @PostMapping
    public ResponseEntity<ServerConfig> createServer(@RequestBody ServerConfig config) {
        return ResponseEntity.ok(serverManagerService.createServer(config));
    }
    
    @PutMapping("/{serverId}")
    public ResponseEntity<Void> updateServer(@PathVariable String serverId, @RequestBody ServerConfig config) {
        config.setId(serverId);
        serverManagerService.updateServer(config);
        return ResponseEntity.ok().build();
    }
    
    @DeleteMapping("/{serverId}")
    public ResponseEntity<Void> deleteServer(@PathVariable String serverId) {
        serverManagerService.deleteServer(serverId);
        return ResponseEntity.ok().build();
    }
    
    @PostMapping("/{serverId}/start")
    public ResponseEntity<Void> startServer(@PathVariable String serverId) {
        serverManagerService.startServer(serverId);
        return ResponseEntity.ok().build();
    }
    
    @PostMapping("/{serverId}/stop")
    public ResponseEntity<Void> stopServer(@PathVariable String serverId) {
        serverManagerService.stopServer(serverId);
        return ResponseEntity.ok().build();
    }
    
    @GetMapping("/{serverId}/connections")
    public ResponseEntity<List<Connection>> getConnections(@PathVariable String serverId) {
        return ResponseEntity.ok(connectionService.getConnectionsByServerId(serverId));
    }
    
    @PostMapping("/connections/{connectionId}/send")
    public ResponseEntity<Void> sendMessage(
            @PathVariable String connectionId,
            @RequestBody Map<String, String> payload) {
        
        String message = payload.get("message");
        String contentType = payload.get("contentType");
        
        if (message == null || contentType == null) {
            return ResponseEntity.badRequest().build();
        }
        
        serverManagerService.sendMessage(connectionId, message, contentType);
        return ResponseEntity.ok().build();
    }
}